<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-select v-model="dataForm.isUsed" filterable placeholder="请选择兑换码是否使用" @change="selectVip">
        <el-option v-for="item in optionsVip" :key="item.status" :label="item.label" :value="item.status">
        </el-option>
      </el-select>
      <el-form-item>
        <el-input v-model="dataForm.activationCode" placeholder="兑换码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="genCode()">生成一个激活码</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column :index="indexMethod" label="#" prop="Serial_number" sortable type="index"></el-table-column>
      <el-table-column prop="activationCode" header-align="center" align="center" label="兑换码">
      </el-table-column>
      <el-table-column prop="isUsed" header-align="center" align="center" label="是否使用">
        <template slot-scope="scope">
          <div v-if="scope.row.isUsed == 1">已使用</div>
          <div v-if="scope.row.isUsed == 0" style="color: green;">未使用</div>
          <div v-if="scope.row.isUsed == 2" style="color: red;">已作废</div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建日期">
      </el-table-column>
      <el-table-column prop="usedTime" header-align="center" align="center" label="使用日期">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <!--  <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <el-button type="text" size="small" @click="copy(scope.row.activationCode)">复制</el-button>

          <el-button type="text" size="small" v-if="scope.row.isUsed == 0"
            @click="deleteHandle(scope.row.id)">作废</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
import AddOrUpdate from "./user-add-or-update";
export default {
  data () {
    return {
      optionsVip: [
        {
          status: "1",
          label: "已使用"
        },
        {
          status: 0,
          label: "未使用"
        },
        {
          status: 2,
          label: "已作废"
        }
      ],
      dataForm: {
        key: "",
        vip: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList();
    this.$http({
      url: this.$http.adornUrl(
        "/theme/tabActivationCode/test?code=J3IOQPXcqAza"
      ),
      method: "get"
    });
  },
  methods: {
    indexMethod (index) {
      return index + 1;
    },
    selectVip (value) {
      this.dataForm.vip = value;
      this.getDataList();
    },
    genCode () {
      this.$http({
        url: this.$http.adornUrl("/theme/tabActivationCode/gen"),
        method: "get"
      }).then(({ data }) => {
        var code = data.msg;
        this.$confirm(`请选择是否复制到剪切板,激活码为：${code}`, "生成成功", {
          confirmButtonText: "复制",
          cancelButtonText: "关闭",
          type: "warning"
        }).then(() => {
          //TODO 复制
          this.$copyText(code).then(
            e => {
              console.log("复制成功：", e);
            },
            e => {
              console.log("复制失败：", e);
            }
          );
        });
      });
      this.dataList = [];
      this.totalPage = 0;
      this.getDataList();
    },
    copy (e) {
      this.$copyText(e).then(
        e => {
          this.$message("复制成功");
          console.log("复制成功：", e);
        },
        e => {
          console.log("复制失败：", e);
        }
      );
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/theme/tabActivationCode/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          isUsed: this.dataForm.isUsed,
          activationCode: this.dataForm.activationCode
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val;
    },
    handlderMobile (row, column) {
      if (row.mobile === null || row.mobile === "") {
        return "-";
      }
      return row.mobile;
    },
    handlderVipExpiresTime (row, column) {
      if (row.vipExpiresTime === null || row.vipExpiresTime === "") {
        return "-";
      }
      return row.vipExpiresTime;
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle (id) {
      this.$http({
        url: this.$http.adornUrl("/theme/tabActivationCode/zf"),
        method: "get",
        params: this.$http.adornParams({
          id: id
        })
      }).then(({ data }) => {
        this.dataList = [];
        this.totalPage = 0;
        this.getDataList();
      });
    }
  }
};
</script>
